﻿package mortal.common.text
{
    import com.mui.containers.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.view.common.*;

    public class AutoLayoutTextContainer extends GBox
    {
        private var _texts:Array;
        private var _neverLayoutIndexs:Array;
        public var layoutByVisible:Boolean = true;
        public static const layoutByVisible_Work:Boolean = true;
        public static const layoutByVisible_Not_Work:Boolean = false;

        public function AutoLayoutTextContainer()
        {
            this._texts = [];
            this.direction = GBoxDirection.VERTICAL;
            this.verticalGap = 5;
            this.layoutByVisible = true;
            return;
        }// end function

        public function addNewText(param1:int = 220, param2:String = "", param3:int = 12, param4:uint = 11661311, param5:String = "left", param6:int = 3, param7:TextFormat = null, param8:Boolean = false) : GTextFiled
        {
            var _loc_9:* = UIFactory.autoSizeGTextFiled(param2, 0, 0, param1, this, param5, param6);
            var _loc_10:* = GlobalStyle.textFormatPutong;
            _loc_10.size = param3;
            _loc_10.color = param4;
            _loc_10.bold = param8;
            _loc_9.defaultTextFormat = _loc_10;
            if (param7 != null)
            {
                _loc_9.defaultTextFormat = param7;
            }
            if (this._texts == null)
            {
                this._texts = [];
            }
            this._texts.push(_loc_9);
            this.resetPosition();
            return _loc_9;
        }// end function

        public function addNeverLayoutIndex(param1:int) : void
        {
            if (this._neverLayoutIndexs == null)
            {
                this._neverLayoutIndexs = [];
            }
            if (this._neverLayoutIndexs.indexOf(param1))
            {
                return;
            }
            this._neverLayoutIndexs.push(param1);
            return;
        }// end function

        public function delNeverLayout(param1:int) : void
        {
            if (this._neverLayoutIndexs == null)
            {
                return;
            }
            var _loc_2:* = this._neverLayoutIndexs.indexOf(param1);
            if (_loc_2 < 0)
            {
                return;
            }
            this._neverLayoutIndexs.splice(_loc_2, 1);
            return;
        }// end function

        override protected function resetPosition() : void
        {
            var _loc_1:int = 0;
            var _loc_4:DisplayObject = null;
            var _loc_2:int = 0;
            var _loc_3:* = this.numChildren;
            if (this.direction == GBoxDirection.VERTICAL)
            {
                _loc_1 = 0;
                while (_loc_1 < _loc_3)
                {
                    
                    _loc_4 = this.getChildAt(_loc_1);
                    if (!this.shouldLayout(_loc_1, _loc_4))
                    {
                    }
                    else
                    {
                        _loc_4.x = 0;
                        _loc_4.y = _loc_2 + verticalGap;
                        _loc_2 = _loc_4.y + _loc_4.height;
                    }
                    _loc_1++;
                }
            }
            else if (this.direction == GBoxDirection.HORIZONTAL)
            {
                _loc_1 = 0;
                while (_loc_1 < _loc_3)
                {
                    
                    _loc_4 = this.getChildAt(_loc_1);
                    if (!this.shouldLayout(_loc_1, _loc_4))
                    {
                    }
                    else
                    {
                        _loc_4.x = _loc_2 + horizontalGap;
                        _loc_4.y = 0;
                        _loc_2 = _loc_4.x + _loc_4.width;
                    }
                    _loc_1++;
                }
            }
            return;
        }// end function

        private function shouldLayout(param1:int, param2:DisplayObject) : Boolean
        {
            if (this._neverLayoutIndexs != null && this._neverLayoutIndexs.indexOf(param1) >= 0)
            {
                return false;
            }
            if (this.layoutByVisible == layoutByVisible_Work && param2.visible == false)
            {
                return false;
            }
            if (param2 is TextField && TextField(param2).text == "")
            {
                return false;
            }
            return true;
        }// end function

        public function split(param1:int) : void
        {
            var _loc_4:GTextFiled = null;
            if (this._texts == null)
            {
                return;
            }
            var _loc_2:* = this._texts.length;
            var _loc_3:* = param1;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = this._texts[_loc_3] as GTextFiled;
                if (_loc_4 != null)
                {
                    _loc_4.dispose(true);
                }
                _loc_3++;
            }
            if (_loc_3 != param1)
            {
                this._texts.splice(param1);
            }
            return;
        }// end function

        public function setText(param1:int, param2:String, param3:Boolean = true) : Boolean
        {
            var _loc_4:* = this.getTextByIndex(param1);
            if (_loc_4 == null)
            {
                return false;
            }
            if (param3)
            {
                _loc_4.htmlText = param2;
            }
            else
            {
                _loc_4.text = param2;
            }
            this.resetPosition();
            return true;
        }// end function

        public function getTextByIndex(param1:int) : GTextFiled
        {
            if (this._texts == null || this._texts.length <= param1)
            {
                return null;
            }
            return this._texts[param1];
        }// end function

        override public function get height() : Number
        {
            var _loc_1:int = 0;
            var _loc_2:DisplayObject = null;
            if (this.direction == GBoxDirection.HORIZONTAL)
            {
                return super.height;
            }
            if (this.direction == GBoxDirection.VERTICAL)
            {
                _loc_1 = this.numChildren - 1;
                while (_loc_1 >= 0)
                {
                    
                    _loc_2 = this.getChildAt(_loc_1);
                    if (this.shouldLayout(_loc_1, _loc_2))
                    {
                        return _loc_2.y + _loc_2.height;
                    }
                    _loc_1 = _loc_1 - 1;
                }
                return 0;
            }
            return super.height;
        }// end function

        override public function get width() : Number
        {
            var _loc_1:int = 0;
            var _loc_2:DisplayObject = null;
            if (this.direction == GBoxDirection.HORIZONTAL)
            {
                _loc_1 = this.numChildren - 1;
                while (_loc_1 >= 0)
                {
                    
                    _loc_2 = this.getChildAt(_loc_1);
                    if (this.shouldLayout(_loc_1, _loc_2))
                    {
                        return _loc_2.x + _loc_2.width;
                    }
                    _loc_1 = _loc_1 - 1;
                }
                return 0;
            }
            else if (this.direction == GBoxDirection.VERTICAL)
            {
                return super.width;
            }
            return super.width;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            this.direction = GBoxDirection.VERTICAL;
            this._texts = [];
            this._neverLayoutIndexs = null;
            this.verticalGap = 5;
            this.layoutByVisible = true;
            return;
        }// end function

    }
}
